<template>
  <div>
    <div class="chart">
      <div ref="chart" :class="className" :style="{height:height,width:width}" />
    </div>
  </div>
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons')
import resize from '../../mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    autoResize: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
      this.query()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    query() {
      this.get('/merchant/getMerchantScale', {}, { showLoading: false }).then(ret => {
        this.setOptions({
          seriesData: [
            { value: ret.autarkyMerchantSum, name: '自营商户数' },
            { value: ret.nonSelfRunSum, name: '非自营商户数' }
          ]
        })
      })
    },
    initChart() {
      this.chart = echarts.init(this.$refs.chart, 'macarons')
      this.setOptions()
    },
    setOptions({ seriesData = [] } = {}) {
      this.chart.clear()
      this.chart.setOption({
        title: {
          text: '自营商户/非自营商户 比例',
          bottom: '2',
          left: 'center',
          textStyle: {
            color: '#999'
          }
        },
        tooltip: {
          trigger: 'item',
          formatter: '{b}: {c} ({d}%)'
        },
        legend: {
          top: '10',
          left: 'center',
          data: ['自营商户数', '非自营商户数']
        },
        series: [
          {
            name: '',
            type: 'pie',
            radius: ['50%', '70%'],
            avoidLabelOverlap: true,
            label: {
              show: true
            },
            labelLine: {
              show: true
            },
            data: seriesData
          }
        ]
      })
    }
  }
}
</script>
